#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/10/18 9:39
# @Author  : afraidbenq
# @File    : connect_devices.py
# @Software: PyCharm
# @Github  : github/afraidbenq
# @Email   : 329189987@qq.com
# @Desc    :
import os

from common import logger


def adb_connect(device_ip):
    """连接设备"""
    if ":" not in device_ip:
        """真机连接"""
        cmd_connect = 'adb connect {}'.format(device_ip)  # 连接设备
        logger.info(cmd_connect)

        # 返回命令执行后的结果
        try:
            result = os.popen(cmd_connect).read()
            logger.info(result)
            if "failed" in result:
                return False
            else:
                return True
        except:
            logger.error("连接{}失败".format(device_ip))
            return False
    elif device_ip.split(":")[1] == "5555":
        """无线连接"""
        cmd_tcpip = 'adb tcpip 5555'
        cmd_connect = 'adb connect {}'.format(device_ip)  # 连接设备
        logger.info(cmd_tcpip)
        logger.info(cmd_connect)
        try:
            result = os.popen(cmd_tcpip).read()
            logger.info(result)
            result1 = os.popen(cmd_connect).read()
            logger.info(result1)
            if "failed" in result1:
                return False
            else:
                return True
        except:
            logger.error("无线连接{}失败".format(device_ip))
            return False
    else:
        """模拟器连接"""
        cmd_connect = 'adb connect {}'.format(device_ip)  # 连接设备
        logger.info(cmd_connect)

        # 返回命令执行后的结果
        try:
            result = os.popen(cmd_connect).read()
            logger.info(result)
            if "failed" in result:
                return False
            else:
                return True
        except:
            logger.error("连接{}失败".format(device_ip))
            return False


def adb_disconnect(device_ip):
    """断开连接"""
    cmd_disconnect = 'adb disconnect {}'.format(device_ip)  # 断开设备
    logger.info(cmd_disconnect)

    result = os.popen(cmd_disconnect).read()
    logger.info(result)


def check_devices(device_ip_list):
    """检测设备连接状态"""
    cmd_devices = 'adb devices'  # 设备列表
    logger.info(cmd_devices)
    try:
        result = os.popen(cmd_devices).read()
        logger.info(result)
    except:
        logger.error("检测设备连接失败")
        return False
    else:
        if "List of devices attached" in result:
            logger.info("检测设备连接情况")
            device_dict = {}
            for i in result.split('\n'):
                if i == "List of devices attached" or i == "":
                    continue
                device_dict[i.split("\t")[0]] = i.split("\t")[1]
            if device_dict:
                device_result = True
                for device_ip in device_ip_list:
                    if device_ip in device_dict:
                        if device_dict[device_ip] == "device":
                            pass
                        else:
                            device_result = False
                    else:
                        logger.error("设备{}没有连接".format(device_ip))
                        device_result = False
                return device_result
            else:
                logger.error("设备{}没有连接".format(device_ip_list))
                return False

        else:
            logger.error("adb服务异常")
            return False


if __name__ == '__main__':
    # adb_connect("127.0.0.1:5555")
    adb_connect("127.0.0.1:62001")
    # adb_disconnect("127.0.0.1:62001")
    # print(check_devices("127.0.0.1:62001"))
